package com.exercises.service.imp;

import com.exercises.mapper.classroom.ClassroomMapper;
import com.exercises.mapper.student.StudentMapper;
import com.exercises.object.*;
import com.exercises.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * --- 代码敲对！ ---
 *
 * @author CCCccc
 * @create 2025/8/15
 * 备注：学生业务层实现类
 */
@Service
public class StudentServiceImp implements StudentService {


    // 注入Mapper
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassroomMapper classroomMapper;

    // 根据id查询学生
    @Override
    public Student getStudentById(int id) {
        System.out.println("id = " + id);
        Student student = studentMapper.getStudentById(id);
        return student;
    }

    // 查询所有学生
    @Override
    public ArrayList<Student> getAllStudents() {
        return studentMapper.getAllStudents();
    }

    // 根据年龄或姓名查询学生
    @Override
    public ArrayList<Student> getStudentByAgeOrName(HashMap<String, String> map) {
        return studentMapper.getStudentByAgeOrName(map);
    }

    // 添加学生
    @Override
    public boolean addStudent(Student student) {
        return studentMapper.addStudent(student);
    }

    // 修改学生
    @Override
    public boolean updateStudent(Student student) {
        return studentMapper.updateStudent(student);
    }

    // 批量删除学生
    @Override
    public boolean deleteStudentBatch(int[] ids) {
        return studentMapper.deleteStudentBatch(ids);
    }

    // 查询学生以及教室信息
    @Override
    public ArrayList<StudentVO> getStudentWithClassroom() {
        return studentMapper.getStudentWithClassroom();
    }

    // 查询学生以及学科信息和任课老师
    @Override
    public ArrayList<StudentVO> getStudentWithSubject() {
        return studentMapper.getStudentWithSubject();
    }

    // 查询学生以及教室信息(单表)
    @Override
    /**
     * 获取包含班级信息的所有学生信息v2版本
     * 该方法通过学生ID查询学生信息，并关联查询对应的班级信息
     *
     * @return ArrayList<StudentVO> 包含班级信息的学生列表
     */
    public ArrayList<StudentVO> getStudentWithClassroomv2() {
        // 刔回结果列表
        ArrayList<StudentVO> studentWithClassroomv2 = new ArrayList<>();
        // 从数据库获取所有学生信息
        ArrayList<Student> students = studentMapper.getAllStudents();
        // 遍历学生列表
        for (Student student : students) {// 根据班级ID获取班级信息
            Classroom classroom = classroomMapper.getClassroomById(student.getCid());
            // 创建学生视图对象
            StudentVO studentVO = new StudentVO();
            // 复制学生属性到视图对象
            BeanUtils.copyProperties(student, studentVO);
            // 设置班级信息到视图对象
            studentVO.setClassroom(classroom);
            // 将视图对象添加到结果列表
            studentWithClassroomv2.add(studentVO);

        }
        // 返回结果列表
        return studentWithClassroomv2;
    }

    // 查询学生以及学科信息(单表)
    @Override
    public StudentVO getStudentWithSubjectv2(int id) {
        // 根据学生id查询学生信息
        Student student = studentMapper.getStudentById(id);
        // 创建学科对象
        ArrayList<Subject> subjectArrayList = new ArrayList<>();
        // 根据学生id查询学科id
        ArrayList<Integer> subjectIds = studentMapper.getSubjectIdsByStudentId(id);
        // 遍历学科id列表
        for (Integer subjectId : subjectIds) {
            // 根据学科id查询学科信息
            Subject subject = studentMapper.getSubjectById(subjectId);
            // 将学科信息添加到学科对象列表
            subjectArrayList.add(subject);
        }
        // 创建学生视图对象
        StudentVO studentVO = new StudentVO();
        // 复制学生属性到视图对象
        BeanUtils.copyProperties(student, studentVO);
        // 设置学科信息到视图对象
        studentVO.setSubjectArrayList(subjectArrayList);
        // 返回结果列表
        return studentVO;
    }

}
